package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.dao.UserFollowMapper;
import com.dhu.dao.UserMapper;
import com.dhu.pojo.User;
import com.dhu.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class UserFollowController {
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private UserFollowMapper userFollowMapper;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("isFollowUser")
    public String isFollowBar (int myId, int userId) {
        JSONObject res = new JSONObject();
        if (userFollowService.isFollowUser(myId, userId) != null) {
            res.put("code", 200);
            res.put("message", "已经关注");
            res.put("isFollowUser", true);
        } else {
            res.put("code", 200);
            res.put("message", "没有关注");
            res.put("isFollowUser", false);
        }
        return res.toJSONString();
    }

    // 改变关注
    @GetMapping("changeFollowUser")
    public String changeFollow (boolean isFollowUser, int myId, int userId) {
        JSONObject res = new JSONObject();
        if (userFollowService.changeFollow(isFollowUser, myId, userId) == 1) {
            res.put("code", 200);
            res.put("message", "更改成功");
        } else {
            res.put("code", 403);
            res.put("message", "更改失败");
        }
        return res.toJSONString();
    }

    // 获取用户关注列表
    @GetMapping("/getFollowUser")
    public String getFollowUser (int id) {
        JSONObject res = new JSONObject();
        // 关注用户的id
        int[] ids = userFollowMapper.selectFollowUser(id);
        if (ids.length == 0) {
            res.put("code", 200);
            res.put("message", "还没有关注");
            return res.toJSONString();
        }
        // 获取关注用户的信息
        List<User> users = new ArrayList<>();
        for (int i : ids) {
            users.add(userMapper.selectById(i));
        }
        if (users.size() != 0) {
            res.put("code", 200);
            res.put("userList", users);
            res.put("message", "查询关注用户成功");
        } else {
            res.put("code", 403);
            res.put("message", "查询关注用户失败");
        }
        return res.toJSONString();
    }

    // 获取用户粉丝列表
    @GetMapping("/getFun")
    public String getFun (int id) {
        JSONObject res = new JSONObject();
        // 粉丝的id
        int[] ids = userFollowMapper.selectFun(id);
        if (ids.length == 0) {
            res.put("code", 200);
            res.put("message", "还没有粉丝");
            return res.toJSONString();
        }
        // 获取粉丝的信息
        List<User> users = new ArrayList<>();
        for (int i : ids) {
            users.add(userMapper.selectById(i));
        }
        if (users.size() != 0) {
            res.put("code", 200);
            res.put("userList", users);
            res.put("message", "查询粉丝成功");
        } else {
            res.put("code", 403);
            res.put("message", "查询粉丝失败");
        }
        return res.toJSONString();
    }
}
